package demo.client.socket;

import java.nio.ByteBuffer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClientSend extends Thread {
	private DemoClient demoClient;
	private Logger logI = LoggerFactory.getLogger("LogInfo");
	private Logger logE = LoggerFactory.getLogger("LogError");

	public ClientSend(DemoClient demoClient) {
		this.demoClient = demoClient;
	}

	@Override
	public void run() {
		String message = "ặặặặặặặặặặ"
//				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
//				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
//				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
//				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
//				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
//				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
//				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
//				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
//				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
//				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
//				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
//				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ" 
				+ "ặặặặặặặặặặặặặặặặặặặ";
		int i = 0;
		while (demoClient.isSend()) {
			i++;
			String newMessage = demoClient.getName() + "_" + i + "_" + message;
			splitMessage(newMessage, i);
			try {
				sleep(50);
			} catch (InterruptedException e) {
				logE.error("", e);
			}
		}
	}

	private void splitMessage(String message, int i) {
		String name = demoClient.getName();
		StringBuffer sb = new StringBuffer();
		sb.append(name).append("_").append(i);
		while (sb.length() < 20) {
			sb.append(" ");
		}
		byte[] bt = message.getBytes();
		int leng = bt.length;

		logI.info("{" + name + "} >>> Start| Type: length | IdMessage: " + name	+ "_" + i + "_ | Size: " + 4);
		if (demoClient.send(leng)) {
			logI.info("{" + name + "} >>> Success| Type: length | IdMessage: " + name + "_" + i + "_ | Size: " + 4);
		} else {
			logI.info("{" + name + "} >>> False| Type: length | IdMessage: "	+ name + "_" + i + "_ | Size: " + 4);
		}
		
		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		ByteBuffer bb = ByteBuffer.allocate(20);
		bb.put((sb.toString()).getBytes());
		bb.position(0);
		byte[] btInfo = new byte[20];
		bb.get(btInfo);
		logI.info("{" + name + "} >>> Start| Type: info | IdMessage: " + name + "_" + i + "_ | Size: " + 20);
		if (demoClient.send(btInfo)) {
			logI.info("{" + name + "} >>> Sucess| Type: info | IdMessage: "	+ name + "_" + i + "_ | Size: " + 20);
		} else {
			logI.info("{" + name + "} >>> False| Type: info | IdMessage: " + name + "_" + i + "_ | Size: " + 20);
		}

		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
			
		bb = ByteBuffer.allocate(leng);
		bb.put(bt);
		bb.position(0);
		int realRead = leng;
		int j = 0;
		while (realRead > 1500) {
			byte[] btTmp = new byte[1500];
			bb.get(btTmp);
			realRead -= 1500;
			logI.info("{" + name + "} >>> Start| Type: data | IdMessage: " + name + "_" + i + "_" + j + " | Size: " + 1500);
			if (demoClient.send(btTmp))
				logI.info("{" + name + "} >>> Success| Type: data | IdMessage: " + name + "_" + i + "_" + j + " | Size: " + 1500);
			else
				logI.info("{" + name + "} >>> False| Type: data | IdMessage " + name + "_" + i + "_" + j + " | Size: " + 1500);
			j++;
			
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		byte[] btTmp = new byte[realRead];
		bb.get(btTmp);
		logI.info("{" + name + "} >>> Start| Type: data | IdMessage: " + name + "_" + i + "_" + j + " | Size: " + btTmp.length);
		if (demoClient.send(btTmp)) {
			logI.info("{" + name + "} >>> Success| Type: data | IdMessage: " + name + "_" + i + "_" + j + " | Size: " + btTmp.length);
		} else {
			logI.info("{" + name + "} >>> False| Type: data | IdMessage: " + name + "_" + i + "_" + j + " | Size: " + btTmp.length);
		}
		

	}
}
